/**
 * 动态规划，存储一维数组
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function(nums) {
    let dp = new Array(nums.length).fill(0),
    max = nums[0];
    dp[0] = nums[0];
    for(let i = 1;i<nums.length;i++){
        dp[i] = Math.max(nums[i], nums[i] + dp[i-1])
        max = Math.max(max,dp[i])
    }

    return max
};


/**
 * 优化一维数组为常量
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function(nums) {
    let previous = nums[0],
    max = nums[0];

    for(let i = 1;i<nums.length;i++){
        previous = Math.max(nums[i], nums[i] + previous)
        max = Math.max(max,previous)
    }

    return max
};


